/****************************************************************************
 * @file     Led_app.c																			  																			  * @brief
 * @version  V5.0.4
 * @date     10. January 2023
 ******************************************************************************/

/*********uart_echo_example.c****************************************/

#include "Led_app.h"
#include "SensorGroup.h"
#include "SysInit.h"
#include "Animation_table.h"
#define TASK_CYCLE_TIME 5 /*uint:ms*/

 uint8_t OldState = INVALID_SIGNAL;//PWM旧状态

volatile PwmInfo_St PwmInfo;

PWM_State_St PWM_State;

AnimationInfo_St AnimationInfo;

/*为每个位置灯PWM通道初始化地址*/
ChannelAddresses CHANNELADDR =
{
        (uint32_t *)&(GPTIM0->CCR1), // GPTIM0通道1的地址   TAIL1
        (uint32_t *)&(GPTIM0->CCR2), // GPTIM0通道2的地址   TAIL2
        (uint32_t *)&(GPTIM1->CCR2), // GPTIM1通道2的地址   TAIL3
};

uint32_t StopPin[2] =
{
        STOP1_PIN,
        STOP2_PIN
};

/* Private macro -------------------------------------------------------------*/

/*****************************************************/

/*****************************************************************************************************************************
 ** Function Name         : SetTail_Logo_ChannelDuty
 **
 ** Service ID            : 0x01
 **
 ** Description           :
 **
 ****************************************************************************************************************************/
void SetTail_Logo_ChannelDuty(uint8_t channel, uint16_t duty) // duty 0-1000,精度0.1%
{
    *CHANNELADDR.channels[channel] = duty;
}

/*****************************************************************************************************************************
 ** Function Name         : SetTailAllON
 **
 ** Service ID            : 0x01
 **
 ** Description           : open all tail led
 **
 ****************************************************************************************************************************/
void SetTailAllON(void)
{
    for(int i = 0; i <= 3; i++)
    {
        SetTail_Logo_ChannelDuty(i,1000);
    }
}

/*****************************************************************************************************************************
 ** Function Name         : SetTail1and2ON
 **
 ** Service ID            : 0x01
 **
 ** Description           : open channel 1 and 2 tail led
 **
 ****************************************************************************************************************************/
void SetTail1and2ON(void)
{
    SetTail_Logo_ChannelDuty(TAIL1_CHANNEL,1000);
    SetTail_Logo_ChannelDuty(TAIL2_CHANNEL,1000);
}

/*****************************************************************************************************************************
 ** Function Name         : SetTailAllOFF
 **
 ** Service ID            : 0x01
 **
 ** Description           :
 **
 ****************************************************************************************************************************/
void SetTailAllOFF(void)
{
    for(int i = 0; i <= 3; i++)
    {
        SetTail_Logo_ChannelDuty(i,0);
    }
}

/*****************************************************************************************************************************
 ** Function Name         : Set_BM_ON
 **
 ** Service ID            : 0x01
 **
 ** Description           :
 **
 ****************************************************************************************************************************/
void Set_BM_ON(void)
{
    SetTail_Logo_ChannelDuty(TAIL1_CHANNEL,1000);
    SetTail_Logo_ChannelDuty(TAIL2_CHANNEL,1000);
    SetTail_Logo_ChannelDuty(TAIL3_CHANNEL,1000);
    FL_GPIO_SetOutputPin(BM_PORT, BM_PIN);
}

/*****************************************************************************************************************************
 ** Function Name         : Set_BM_OFF
 **
 ** Service ID            : 0x01
 **
 ** Description           :
 **
 ****************************************************************************************************************************/
void Set_BM_OFF(void)
{
    FL_GPIO_ResetOutputPin(BM_PORT, BM_PIN);
}

/*****************************************************************************************************************************
 ** Function Name         : Delay_MS
 **
 ** Service ID            : 0x01
 **
 ** Description           : This function Delay_MS
 **
 ****************************************************************************************************************************/
void Delay_MS(void)
{
    uint16_t k = 0;

    for (k = 0; k < 4000; k++) // delay10ms
    {
        ;
    }
}

/*****************************************************************************************************************************
 ** Function Name         : Led_App_CloseAllLight
 **
 ** Service ID            : 0x01
 **
 ** Description           : This function Close All Light
 **
 ****************************************************************************************************************************/
void Led_App_CloseAllLight(void)
{
    SetTailAllOFF();
    Set_Stop_OFF();
}
/*****************************************************************************************************************************
 ** Function Name         : GET_TAIL_ON_OFF_SIGN
 **
 ** Service ID            : 0x01
 **
 ** Description           : This function TAIL_IN FUNC IS DETECED
 **
 ****************************************************************************************************************************/
SwitchStatusEnum GET_TAIL_ON_OFF_SIGN(void)
{
    SwitchStatusEnum ret = SWT_OFF;
    static SwitchStatusEnum BatFlag = SWT_OFF;

    if ((tail_detect.PhysicVal >= LIGHT_ON_LOW_VOLT) && (tail_detect.PhysicVal <= LIGHT_ON_HIGH_VOLT))
    {
        ret = SWT_ON;
        BatFlag = SWT_ON;
    }
    else if ((tail_detect.PhysicVal <= LIGHT_OFF_LOW_VOLT) || (tail_detect.PhysicVal >= LIGHT_OFF_HIGH_VOLT))
    {
        ret = SWT_OFF;
        BatFlag = SWT_OFF;
    }
    else
    {
        if (BatFlag == SWT_ON)
        {
            ret = SWT_ON;
        }
    }
    return ret;
}

/*****************************************************************************************************************************
 ** Function Name         : GET_LOGO_ON_OFF_SIGN
 **
 ** Service ID            : 0x01
 **
 ** Description           : This function STOP_IN FUNC IS DETECED
 **
 ****************************************************************************************************************************/
SwitchStatusEnum GET_STOP_ON_OFF_SIGN(void)
{
    SwitchStatusEnum ret = SWT_OFF;
    static SwitchStatusEnum BatFlag = SWT_OFF;

    if ((stop_detect.PhysicVal >= LIGHT_ON_LOW_VOLT) && (stop_detect.PhysicVal <= LIGHT_ON_HIGH_VOLT))
    {
        ret = SWT_ON;
        BatFlag = SWT_ON;
    }
    else if ((stop_detect.PhysicVal <= LIGHT_OFF_LOW_VOLT) || (stop_detect.PhysicVal >= LIGHT_OFF_HIGH_VOLT))
    {
        ret = SWT_OFF;
        BatFlag = SWT_OFF;
    }
    else
    {
        if (BatFlag == SWT_ON)
        {
            ret = SWT_ON;
        }
    }
    return ret;
}

/*****************************************************************************************************************************
 ** Function Name         : GET_LOGO_ON_OFF_SIGN
 **
 ** Service ID            : 0x01
 **
 ** Description           : This function LOGO_IN FUNC IS DETECED
 **
 ****************************************************************************************************************************/
SwitchStatusEnum GET_LOGO_ON_OFF_SIGN(void)
{
    SwitchStatusEnum ret = SWT_OFF;
    static SwitchStatusEnum BatFlag = SWT_OFF;

    if ((logo_detect.PhysicVal >= LIGHT_ON_LOW_VOLT) && (logo_detect.PhysicVal <= LIGHT_ON_HIGH_VOLT))
    {
        ret = SWT_ON;
        BatFlag = SWT_ON;
    }
    else if ((logo_detect.PhysicVal <= LIGHT_OFF_LOW_VOLT) || (logo_detect.PhysicVal >= LIGHT_OFF_HIGH_VOLT))
    {
        ret = SWT_OFF;
        BatFlag = SWT_OFF;
    }
    else
    {
        if (BatFlag == SWT_ON)
        {
            ret = SWT_ON;
        }
    }
    return ret;
}

/*****************************************************************************************************************************
 ** Function Name		  : Led_App_Animation_Det
 **
 ** Service ID			  : 0x02
 **
 ** Description 		  : This function Led_App_Task
 **
 ****************************************************************************************************************************/
void Led_App_Animation_Det(uint32_t freq, uint32_t duty)
{
    uint8_t SIGNAL_VALID_CNT = 6;
    uint8_t i = 0;
    PwmInfo.freq = freq;
    PwmInfo.duty = duty;
    PwmInfo.timeout = 0;
    if (((PwmInfo.freq >= 90) && (PwmInfo.freq <= 130)) && ((PwmInfo.duty >= 335) && (PwmInfo.duty <= 465)))
    {
        PwmInfo.timeIn[ANIMATION_WELCOME]++;
        if (PwmInfo.timeIn[ANIMATION_WELCOME] >= SIGNAL_VALID_CNT)
        {
            PwmInfo.timeIn[ANIMATION_WELCOME] = SIGNAL_VALID_CNT;
            PwmInfo.type = WELCOME_SIGNAL;
        }
        PwmInfo.timeIn[ANIMATION_GOODBYE] = 0;
        PwmInfo.timeIn[ANIMATION_TRUNK_OPEN] = 0;
    }
    else if (((PwmInfo.freq >= 90) && (PwmInfo.freq <= 130)) && ((PwmInfo.duty >= 785) && (PwmInfo.duty <= 915)))
    {
        PwmInfo.timeIn[ANIMATION_GOODBYE]++;
        if (PwmInfo.timeIn[ANIMATION_GOODBYE] >= SIGNAL_VALID_CNT)
        {
            PwmInfo.timeIn[ANIMATION_GOODBYE] = SIGNAL_VALID_CNT;
            PwmInfo.type = GOODBYE_SIGNAL;
        }
        PwmInfo.timeIn[ANIMATION_WELCOME] = 0;
        PwmInfo.timeIn[ANIMATION_TRUNK_OPEN] = 0;
    }
    else if (((PwmInfo.freq >= 90) && (PwmInfo.freq <= 130)) && ((PwmInfo.duty >= 485) && (PwmInfo.duty <= 615)))
    {
        PwmInfo.timeIn[ANIMATION_TRUNK_OPEN]++;
        if (PwmInfo.timeIn[ANIMATION_TRUNK_OPEN] >= SIGNAL_VALID_CNT)
        {
            PwmInfo.timeIn[ANIMATION_TRUNK_OPEN] = SIGNAL_VALID_CNT;
            PwmInfo.type = TRUNK_OPEN_SIGNAL;
        }
        PwmInfo.timeIn[ANIMATION_WELCOME] = 0;
        PwmInfo.timeIn[ANIMATION_GOODBYE] = 0;
    }
    /* Add your code here...*/
    else
    {
        PwmInfo.timeIn[ANIMATION_INVALID]++;
        if (PwmInfo.timeIn[ANIMATION_INVALID] >= SIGNAL_VALID_CNT)
        {
            PwmInfo.timeIn[ANIMATION_INVALID] = SIGNAL_VALID_CNT;
            PwmInfo.type = INVALID_SIGNAL;
        }
        for (i = 0; i < ANIMATION_INVALID; i++)
        {
            PwmInfo.timeIn[i] = 0;
        }
    }
}

/*****************************************************************************************************************************
 ** Function Name		  : Led_App_Signal
 **
 ** Service ID			  : 0x02
 **
 ** Description 		  : This function Led_App_Signal
 **
 ****************************************************************************************************************************/
void Led_App_Signal(void)
{
    if (AnimationInfo.Status != ANIMATION_IDLE)
    {
        /*when in Animation ongoing or wait for Animation signal in 30ms*/
      
        return;
    }

    if ((GET_STOP_ON_OFF_SIGN() == SWT_ON) && (GET_TAIL_ON_OFF_SIGN() == SWT_OFF))
    {
        Set_Stop_ON();
        SetTail1and2ON();
        Set_BM_OFF();
    }
    else if((GET_STOP_ON_OFF_SIGN() == SWT_ON) && (GET_TAIL_ON_OFF_SIGN() == SWT_ON))
    {
        Set_Stop_ON();
        SetTailAllON();
        Set_BM_OFF();
    }
    else if(GET_TAIL_ON_OFF_SIGN() == SWT_ON)
    {
        Set_Stop_OFF();
        SetTailAllON();
        Set_BM_OFF();
    }
    else 
    {
        Set_Stop_OFF();
        SetTailAllOFF();
        Set_BM_OFF();
    }
}

/*****************************************************************************************************************************
 ** Function Name		  : Led_Init
 **
 ** Service ID			  : 0x02
 **
 ** Description 		  : This function Led_init
 **
 ****************************************************************************************************************************/
void Led_App_Init(void)
{
    uint8_t i = 0;

    PwmInfo.type = INVALID_SIGNAL;
    PwmInfo.duty = 0;
    PwmInfo.freq = 0;
    PwmInfo.timeout = 0;

    for (i = 0; i < ANIMATION_MAXNUM; i++)
    {
        PwmInfo.timeIn[i] = 0;
    }

    AnimationInfo.FirstFlag = 1;
    AnimationInfo.WaitTime = 6; /*5ms*6 = 30ms */
}

/*****************************************************************************************************************************
 ** Function Name		  : Led_App_Mian
 **
 ** Service ID			  : 0x02
 **
 ** Description 		  : This function Led_App_Main
 **
 ****************************************************************************************************************************/
void Led_App_Main(void)
{
    Led_App_Signal();
}

/*****************************************************************************************************************************
 ** Function Name		  : Led_App_Animation_Stop_Det
 **
 ** Service ID			  : 0x02
 **
 ** Description 		  : This function Led_App_Animation_Stop_Det
 **
 ****************************************************************************************************************************/
void Led_App_Animation_Stop_Det(void)
{
    AnimationInfo_St *p = &AnimationInfo;
    uint8_t SIGNAL_INVALID_CNT = 20;
    uint8_t i = 0;

    PwmInfo.timeout++;

    if (PwmInfo.timeout > SIGNAL_INVALID_CNT)
    {
        PwmInfo.timeout = SIGNAL_INVALID_CNT;
        PwmInfo.type = NO_SIGNAL;
        PWM_State.NewState = INVALID_SIGNAL;

        for (i = 0; i < ANIMATION_MAXNUM; i++)
        {
            PwmInfo.timeIn[i] = 0;
        }
    }

    /*Can quit by other hardware signal interrupt*/
    if (GET_STOP_ON_OFF_SIGN() == SWT_ON && PwmInfo.type != TRUNK_OPEN_SIGNAL)
    {
        p->Status = ANIMATION_IDLE;
        PWM_State.NewState = INVALID_SIGNAL;
    }
}

/*****************************************************************************************************************************
 ** Function Name		  : Welcome_Act
 **
 ** Service ID			  : 0x02
 **
 ** Description 		  : This function Welcome_Act
 **
 ****************************************************************************************************************************/
void Welcome_Act(void)
{
    AnimationInfo_St *p = &AnimationInfo;

    if (p->WelcomeFlag == ANIMATION_WELCOME)
    {
        
       Set_BM_OFF();  
      
    
            if (p->WelcomeCounter == WelcomeTimeTab[p->WelcomeStep])
            {
                SetTail_Logo_ChannelDuty(TAIL1_CHANNEL, WelcomeDataTab[p->WelcomeStep][TAIL1_CHANNEL]);
                SetTail_Logo_ChannelDuty(TAIL2_CHANNEL, WelcomeDataTab[p->WelcomeStep][TAIL2_CHANNEL]);
                SetTail_Logo_ChannelDuty(TAIL3_CHANNEL, WelcomeDataTab[p->WelcomeStep][TAIL3_CHANNEL]);
                for (int i = 0; i < 2; i++)
                {
                    if (WelcomeDataTab[p->WelcomeStep][i] == STOP_ON)
                    {
                        FL_GPIO_SetOutputPin(STOP_PORT, StopPin[i]);
                    }
                    else
                    {
                        FL_GPIO_ResetOutputPin(STOP_PORT, StopPin[i]);
                    }
                }
                if (p->WelcomeStep < WELCOME_MAXSTEP_NUM)
                {
                    p->WelcomeStep++;
                }
            }
            if (p->WelcomeCounter <= WelcomeTimeTab[p->WelcomeStep])
            {
                p->WelcomeCounter += TASK_CYCLE_TIME;
            }
        }
}

/*****************************************************************************************************************************
 ** Function Name		  : Goodbye_Act
 **
 ** Service ID			  : 0x02
 **
 ** Description 		  : This function Goodbye_Act
 **
 ****************************************************************************************************************************/
void Goodbye_Act(void)
{
    AnimationInfo_St *p = &AnimationInfo;

    if ((p->GoodbyeFlag == ANIMATION_GOODBYE)|| (p->GoodbyeFlag == ANIMATION_TRUNK_CLOSE))
    {
      
      Set_BM_OFF();  
      
        if (p->GoodbyeCounter == GoodbyeTimeTab[p->GoodbyeStep])
        {
                /*Animation welcome code*/
                SetTail_Logo_ChannelDuty(TAIL1_CHANNEL, GoodbyeDataTab[p->GoodbyeStep][TAIL1_CHANNEL]);
                SetTail_Logo_ChannelDuty(TAIL2_CHANNEL, GoodbyeDataTab[p->GoodbyeStep][TAIL2_CHANNEL]);
                SetTail_Logo_ChannelDuty(TAIL3_CHANNEL, GoodbyeDataTab[p->GoodbyeStep][TAIL3_CHANNEL]);
                for (int i = 0; i < 2; i++)
                {
                    if (GoodbyeDataTab[p->GoodbyeStep][i] == STOP_ON)
                    {
                        FL_GPIO_SetOutputPin(STOP_PORT, StopPin[i]);
                    }
                    else
                    {
                        FL_GPIO_ResetOutputPin(STOP_PORT, StopPin[i]);
                    }
                }
            if (p->GoodbyeStep < GOODBYE_MAXSTEP_NUM)
            {
                p->GoodbyeStep++;
            }
        }
        if (p->GoodbyeCounter <= GoodbyeTimeTab[p->GoodbyeStep])
        {
            p->GoodbyeCounter += TASK_CYCLE_TIME;
        }
    }
}

/*****************************************************************************************************************************
 ** Function Name		  : Trunk_Open_Act
 **
 ** Service ID			  : 0x02
 **
 ** Description 		  : This function Trunk_Open_Act
 **
 ****************************************************************************************************************************/
void Trunk_Open_Act(void)
{
    AnimationInfo_St *p = &AnimationInfo;

    if (p->WelcomeFlag == ANIMATION_TRUNK_OPEN)
    {
        if (GET_STOP_ON_OFF_SIGN() == SWT_ON)
        {
            p->WelcomeCounter = 4085;
            p->WelcomeStep = WELCOME_MAXSTEP_NUM - 1;
            Set_Stop_ON();
            SetTailAllON();
            Set_BM_OFF();
        }


            if (p->WelcomeCounter == WelcomeTimeTab[p->WelcomeStep])
            {
                SetTail_Logo_ChannelDuty(TAIL1_CHANNEL, WelcomeDataTab[p->WelcomeStep][TAIL1_CHANNEL]);
                SetTail_Logo_ChannelDuty(TAIL2_CHANNEL, WelcomeDataTab[p->WelcomeStep][TAIL2_CHANNEL]);
                SetTail_Logo_ChannelDuty(TAIL3_CHANNEL, WelcomeDataTab[p->WelcomeStep][TAIL3_CHANNEL]);
                for (int i = 0; i < 2; i++)
                {
                    if (WelcomeDataTab[p->WelcomeStep][i] == STOP_ON)
                    {
                        FL_GPIO_SetOutputPin(STOP_PORT, StopPin[i]);
                    }
                    else
                    {
                        FL_GPIO_ResetOutputPin(STOP_PORT, StopPin[i]);
                    }
                }
                if (p->WelcomeStep < WELCOME_MAXSTEP_NUM)
                {
                    p->WelcomeStep++;
                }
            }
            if (p->WelcomeCounter <= WelcomeTimeTab[p->WelcomeStep])
            {
                p->WelcomeCounter += TASK_CYCLE_TIME;
            }
            else if (((p->WelcomeCounter >= WelcomeTimeTab[WELCOME_MAXSTEP_NUM - 1])) && (GET_STOP_ON_OFF_SIGN() == SWT_OFF))
            {
                Set_Stop_OFF();
                Set_BM_ON();
            }
        }
}

/*****************************************************************************************************************************
 ** Function Name		  : Animation_Action
 **
 ** Service ID			  : 0x02
 **
 ** Description 		  : 
 **
 ****************************************************************************************************************************/
void Animation_Action(void)
{
    Welcome_Act();
    Goodbye_Act();
    Trunk_Open_Act();
}
/*****************************************************************************************************************************
 ** Function Name		  : Animation_Main
 **
 ** Service ID			  : 0x02
 **
 ** Description 		  : This function Animation_Main
 **
 ****************************************************************************************************************************/
void Animation_Main(void)
{
    AnimationInfo_St *p = &AnimationInfo;
    static PwmType_Em PrevTpe = INVALID_SIGNAL;

    switch (p->Status)
    {
    case ANIMATION_INIT:
        if (p->WaitTime > 0)
        {
            p->WaitTime--;
        }
        else
        {
            if (p->FirstFlag == 1)
            {
                if ((PwmInfo.timeIn[ANIMATION_WELCOME]) || (PwmInfo.timeIn[ANIMATION_GOODBYE]) || (PwmInfo.timeIn[ANIMATION_TRUNK_OPEN]))
                {
                    p->WaitTime = 6; /*30ms*/
                }
                p->FirstFlag = 0;
            }
            else
            {
                p->Status = ANIMATION_IDLE;
            }
        }

        if (PwmInfo.type != INVALID_SIGNAL)
        {
            p->Status = ANIMATION_PREPARE;
        }
        break;

    case ANIMATION_PREPARE:
        //Led_App_CloseAllLight();

        p->WaitTime = 0;
        p->GoodbyeStep = 0;
        p->WelcomeStep = 0;
        p->GoodbyeCounter = 0;
        p->WelcomeCounter = 0;
        p->Status = ANIMATION_ONGOING;

        if (PwmInfo.type == WELCOME_SIGNAL)
        {
            p->WelcomeFlag = ANIMATION_WELCOME;
            p->GoodbyeFlag = ANIMATION_INVALID;
        }
        else if (PwmInfo.type == GOODBYE_SIGNAL)
        {
            p->WelcomeFlag = ANIMATION_INVALID;
            p->GoodbyeFlag = ANIMATION_GOODBYE;
        }
        else if (PwmInfo.type == TRUNK_OPEN_SIGNAL)
        {
            p->WelcomeFlag = ANIMATION_TRUNK_OPEN;
            p->GoodbyeFlag = ANIMATION_INVALID;
        }
        else if (PWM_State.NewState == TRUNK_CLOSE_SIGNAL)
        {
            p->WelcomeFlag = ANIMATION_INVALID;
            p->GoodbyeFlag = ANIMATION_TRUNK_CLOSE;
        }
        else
        {
            p->WelcomeFlag = ANIMATION_INVALID;
            p->GoodbyeFlag = ANIMATION_INVALID;
        }

        break;

    case ANIMATION_ONGOING:
        Animation_Action();
        if ((PwmInfo.type == INVALID_SIGNAL) || ((PwmInfo.type == NO_SIGNAL) &&  (PWM_State.NewState != TRUNK_CLOSE_SIGNAL)))
        {
            p->Status = ANIMATION_IDLE;
        }
        else
        {
            if (PrevTpe != PwmInfo.type)
            {
                p->Status = ANIMATION_PREPARE;
            }
        }
        break;

    case ANIMATION_IDLE:
        if ((PwmInfo.type == WELCOME_SIGNAL) \
          || (PwmInfo.type == GOODBYE_SIGNAL) \
            ||  (PwmInfo.type == TRUNK_OPEN_SIGNAL) \
              || ((PwmInfo.type == NO_SIGNAL) &&  (PWM_State.NewState == TRUNK_CLOSE_SIGNAL )))
        {
            p->Status = ANIMATION_PREPARE;
        }
        break;

    default:
        break;
    }
    PrevTpe = PwmInfo.type;
}

/*****************************************************************************************************************************
 ** Function Name		  : Change_TrunkAnimatio_State
 **
 ** Service ID			  : 0x02
 **
 ** Description 		  : 
 **
 ****************************************************************************************************************************/
void Change_TrunkAnimatio_State(void)
{
    static uint16_t TimeCnt = 0;
    if((PwmInfo.type != INVALID_SIGNAL) && (PwmInfo.type != NO_SIGNAL))
    {
        PWM_State.OldState = PwmInfo.type;
    }

    if((PWM_State.OldState == TRUNK_OPEN_SIGNAL) && (PwmInfo.type == NO_SIGNAL))
    {
        PwmInfo.timeout = 0;
        PWM_State.NewState = TRUNK_CLOSE_SIGNAL;
    }
    else
    {
       PWM_State.NewState = INVALID_SIGNAL;
       TimeCnt = 0;
    }

    if((TimeCnt < 1000) && (PWM_State.NewState == TRUNK_CLOSE_SIGNAL))
    {
        PwmInfo.timeout = 0;
        TimeCnt++;
    }
    else if(TimeCnt >= 1000)
    {
        PWM_State.NewState = INVALID_SIGNAL;
        PWM_State.OldState = PWM_State.NewState;
        TimeCnt = 0;
    }
   
}

/****************************************************************************************************************************
 * End of file
 **********************************************************************************************************************/
